home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / PowerPlant / LProgressIndicator & Friends / Threads App Source / CThreadsApp.cp < prev    next >
Encoding:
Text File  |  1996-04-21  |  5.9 KB  |  266 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CThreadsApp.cp                ©1996 Metrowerks Inc. All rights reserved.
  3. // =================================================================================
  4.  
  5. #include <LGrowZone.h>
  6. #include <LString.h>
  7. #include <LThread.h>
  8. #include <LWindow.h>
  9. #include <PP_Messages.h>
  10. #include <PPobClasses.h>
  11. #include <UDesktop.h>
  12. #include <UDrawingState.h>
  13. #include <UEnvironment.h>
  14. #include <UMemoryMgr.h>
  15. #include <UReanimator.h>
  16. #include <URegistrar.h>
  17. #include <UThread.h>
  18. #include "LSingleLineCaption.h"
  19.  
  20. #include "ThreadsConstants.h"
  21. #include "LThermometerPane.h"
  22. //#include "CProgressPane.h"
  23. #include "CThreadWindow.h"
  24.  
  25. #include "CThreadsApp.h"
  26.  
  27. #include "LProgressDialog.h"
  28. #include "LAnimateCursor.h"
  29.  
  30. // =================================================================================
  31. //        • Main Program
  32. // =================================================================================
  33.  
  34. void
  35. main( void )
  36. {
  37.     // Initialize the heap. Parameter is number
  38.     // of master handle blocks to allocate.
  39.     InitializeHeap( 4 );
  40.     
  41.     // Initialize the MacOS toolbox.
  42.     UQDGlobals::InitializeToolbox( &qd );
  43.  
  44.     // Check for thread manager.
  45.     if ( !UEnvironment::HasFeature( env_HasThreadsManager ) ) {
  46.     
  47.         ::StopAlert( rALRT_NoThreadManager, nil );
  48.         ::ExitToShell();
  49.     }
  50.     
  51.     // Install a GrowZone function to catch low memory situations.
  52.     // Parameter is the size of the memory reserve in bytes.
  53.     new LGrowZone( 20000 );
  54.  
  55.     // Create the application object and run it.
  56.     CThreadsApp    theApp;
  57.     theApp.Run();
  58. }
  59.  
  60.  
  61. // ---------------------------------------------------------------------------------
  62. //        • CThreadsApp
  63. // ---------------------------------------------------------------------------------
  64.  
  65. CThreadsApp::CThreadsApp()
  66. {
  67.     // Setup the throw and signal actions.
  68.     SetDebugThrow_( debugAction_Alert );
  69.     SetDebugSignal_( debugAction_Alert );
  70.  
  71.     // Register PowerPlant class creator functions.
  72.     RegisterAllPPClasses();
  73.  
  74.     // Register custom classes.
  75.     URegistrar::RegisterClass( 'PrgP',
  76.         LThermometerPane::CreateThermometerFromStream );
  77.     URegistrar::RegisterClass( CThreadWindow::class_ID,
  78.         CThreadWindow::CreateThreadWindowStream );
  79.     URegistrar::RegisterClass( LSingleLineCaption::class_ID,
  80.         LSingleLineCaption::CreateCaptionStream );
  81.  
  82.     // Create the main thread.
  83.     new UMainThread;
  84.  
  85.     // Add a yield attachment.
  86.     AddAttachment( new LYieldAttachment( -1 ) );
  87.     
  88.     // Set sleep time to a low value.
  89.     SetSleepTime( 1 );
  90. }
  91.  
  92.  
  93. // ---------------------------------------------------------------------------------
  94. //        • ~CThreadsApp
  95. // ---------------------------------------------------------------------------------
  96.  
  97. CThreadsApp::~CThreadsApp()
  98. {
  99. }
  100.  
  101.  
  102. // ---------------------------------------------------------------------------------
  103. //        • StartUp
  104. // ---------------------------------------------------------------------------------
  105.  
  106. void
  107. CThreadsApp::StartUp()
  108. {
  109.     ObeyCommand( 'nprg', nil );
  110. }
  111.  
  112.  
  113. // ---------------------------------------------------------------------------------
  114. //        • ObeyCommand
  115. // ---------------------------------------------------------------------------------
  116.  
  117. Boolean
  118. CThreadsApp::ObeyCommand(
  119.     CommandT    inCommand,
  120.     void        *ioParam )
  121. {
  122.     Boolean    cmdHandled = true;
  123.  
  124.     switch ( inCommand ) {
  125.  
  126.         case cmd_New:
  127.         {
  128.             Try_{
  129.             
  130.                 // Make a new window.
  131.                 LWindow    *theWindow;
  132.                 theWindow = LWindow::CreateWindow( rPPob_ThreadsWindow, this );
  133.                 ThrowIfNil_( theWindow );
  134.  
  135.                 // Show the window.
  136.                 theWindow->Show();
  137.             
  138.             } Catch_( inErr ) {
  139.             
  140.                 // Oops, couldn't create the window.
  141.             
  142.             } EndCatch_
  143.         }
  144.         break;
  145.         
  146.         //
  147.         // test LProgressDialog
  148.         //
  149.         case 'nprg':
  150.         {
  151.             LProgressDialog        progress("\pTimeless", this);
  152.             LAnimateCursor        cursor(256, 4);
  153.             
  154.             //
  155.             // LProgressDialog starts life invisible
  156.             // to avoid title flashing
  157.             //
  158.             
  159.             progress.Show();
  160.             
  161.             UInt32    ticksAfterOne, ticksAfterTwo;
  162.             
  163.             progress.SetActionDescriptor("\pLet’s shock the monkey");
  164.             
  165.             //
  166.             // generate picture of ronald reagan
  167.             //
  168.             progress.SetStepDescriptor("\pGenerating picture of nude Ronald Reagon…");
  169.             
  170.             ticksAfterOne = TickCount() + 50;
  171.             ticksAfterTwo = TickCount() + 100;
  172.             
  173.             progress.SetTaskType(task_Indeterminate);        // start with stripes
  174.             
  175.             while(ticksAfterOne > TickCount() &&
  176.                         !progress.IsStopClicked())
  177.             {
  178.                 ProcessNextEvent();
  179.                 progress.CompletedThisMuch(ticksAfterTwo - TickCount());
  180.                 cursor.Set();
  181.             }
  182.             
  183.             //
  184.             // show it to the monkey
  185.             //
  186.             progress.SetStepDescriptor("\pShowing picture to the monkey…");
  187.             
  188.             ticksAfterTwo = TickCount() + 100;
  189.             progress.SetTaskType(task_Measured);            // switch to progress bar
  190.             
  191.             while(ticksAfterTwo > TickCount() &&
  192.                         !progress.IsStopClicked())
  193.             {
  194.                 ProcessNextEvent();
  195.                 progress.CompletedThisMuch(ticksAfterTwo - TickCount());
  196.                 cursor.Set();
  197.             }
  198.             
  199.             //
  200.             // shore up PowerPlant's command hierarchy
  201.             //
  202.             
  203.             LCommander::SwitchTarget(this);
  204.             
  205.         }
  206.         break;
  207.         
  208.         default:
  209.         {
  210.             // Call inherited.
  211.             cmdHandled = LApplication::ObeyCommand( inCommand, ioParam );
  212.         }
  213.         break;
  214.  
  215.     }
  216.  
  217.     return cmdHandled;
  218. }
  219.  
  220. //
  221. // override adjustcursor -- this is necessary so InitCursor
  222. // won't be called while we're spinning the cursor
  223. //
  224.  
  225. void CThreadsApp::AdjustCursor(const EventRecord &inMacEvent)
  226. {
  227.     if(!LCursor::IsLocked())
  228.     {
  229.         LApplication::AdjustCursor(inMacEvent);
  230.     }
  231. }
  232.  
  233.  
  234. // ---------------------------------------------------------------------------------
  235. //        • FindCommandStatus
  236. // ---------------------------------------------------------------------------------
  237.  
  238. void
  239. CThreadsApp::FindCommandStatus(
  240.     CommandT    inCommand,
  241.     Boolean        &outEnabled,
  242.     Boolean        &outUsesMark,
  243.     Char16        &outMark,
  244.     Str255        outName )
  245. {
  246.     switch ( inCommand ) {
  247.  
  248.         case cmd_New:
  249.         case 'nprg':
  250.         {
  251.             // Enable the new command.
  252.             outEnabled = true;
  253.         }
  254.         break;
  255.  
  256.         default:
  257.         {
  258.             // Call inherited.
  259.             LApplication::FindCommandStatus( inCommand,
  260.                 outEnabled, outUsesMark, outMark, outName );
  261.         }
  262.         break;
  263.  
  264.     }
  265. }
  266.